//
//  MyAccountController.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/8/7.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "MyAccountController.h"
#import "MyAccountCell.h"
#import "MyAccountDetailController.h"
#import "MyAccountListModel.h"
#import "ShenHeFinishedViewController.h"
@interface MyAccountController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topNavBarConstraint;
@property (weak, nonatomic) IBOutlet UILabel *totalMoneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *securityFundLbl;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation MyAccountController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self requestListData];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
#pragma mark - 私有方法
- (void)setupUI {
    self.topNavBarConstraint.constant = kNaviHeight;
    
    self.page = 1;
    self.tableView.rowHeight = 60;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestListData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self setTableViewSeperaInsetZero];
}
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 网络请求
- (void)requestListData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNumber"] = @1;
    [NetWorkTool POST:getBalanceDetails param:param success:^(id dic) {
        
        self.totalMoneyLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"data"][@"balance"]];
        self.securityFundLbl.text = [NSString stringWithFormat:@"￥%@",dic[@"data"][@"money"]];
        self.dataArray = [MyAccountListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"detailsList"]];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        self.page = 2;
        self.dataArray = [self.dataArray mutableCopy];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
- (void)loadMoreData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageNumber"] = @(self.page);
    [NetWorkTool POST:getBalanceDetails param:param success:^(id dic) {
        NSMutableArray *modelArray = [MyAccountListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"detailsList"]];
        if (modelArray.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        self.page++;
        [self.dataArray addObjectsFromArray:modelArray];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } fail:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } needUser:YES];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyAccountListModel *model = self.dataArray[indexPath.row];
    MyAccountCell *cell = [MyAccountCell cellWithTableView:tableView];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAccountListModel *model = self.dataArray[indexPath.row];
    if ([model.type isEqualToString:@"1"]) {//1充值 2补助金扣除
        MyAccountDetailController * vc = [[MyAccountDetailController alloc] init];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    } else {//跳转到补助金详情
        ShenHeFinishedViewController *vc = [[ShenHeFinishedViewController alloc]init];
        vc.idStr = model.grantsId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
// cell的分割线顶格
- (void)setTableViewSeperaInsetZero{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}
@end
